package com.itsxb.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsxb.constants.Constants;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.mapper.DriverAuthMaterialMapper;
import com.itsxb.mapper.DriverMapper;
import com.itsxb.mapper.DriverMaterialAuthLogMapper;
import com.itsxb.mapper.DriverSummaryMapper;
import com.itsxb.pojo.domain.Driver;
import com.itsxb.pojo.domain.DriverAuthMaterial;
import com.itsxb.pojo.domain.DriverMaterialAuthLog;
import com.itsxb.pojo.domain.DriverSummary;
import com.itsxb.pojo.dto.AuditAuthDto;
import com.itsxb.pojo.dto.CancelAuditDto;
import com.itsxb.pojo.vo.DriverWorkbenchVo;
import com.itsxb.service.IDriverAuthMaterialService;
import com.itsxb.utils.AssertUtil;
import com.itsxb.utils.BitStatesUtil;

import java.util.Collections;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author 奇天书
 * @since 2023-12-22
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private DriverMaterialAuthLogServiceImpl driverMaterialAuthLogService;
    @Autowired
    private DriverSummaryMapper driverSummaryMapper;

    //审批认证业务逻辑
    @Override
    public void auditAuth(AuditAuthDto auditAuthDto) {
        //1.参数校验JSR303
        //1.1 校验前端传过来的参数是否为空
        //2.业务校验
        //2.1司机认证材料信息是否能查到
        DriverAuthMaterial driverAuthMaterial = getById(auditAuthDto.getId());
        AssertUtil.isNotNull(driverAuthMaterial, GlobalExceptionCode.PARAM_ERROR);
        //2.2 司机认证材料信息是否为审核中
        AssertUtil.isTrue(Constants.AuditStatus.AUDITING.equals(driverAuthMaterial.getRealAuthStatus()), GlobalExceptionCode.SERVICE_ERROR);
        //3.业务实现
        //因为要先去查询driver表
        Driver driver = driverMapper.selectById(driverAuthMaterial.getDriverId());
        //3.1 判断audiAuthDto返回的是approve是true还是false 还有不同的情况就是 对于driver表的位状态的修改 在true和false 也是不同的
        if (auditAuthDto.getApprove()) {
            driverAuthMaterial.setRealAuthStatus(Constants.AuditStatus.AUDITED_SUCCESS);
            driver.setBitState(BitStatesUtil.addState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATIONED));
        } else {
            driverAuthMaterial.setRealAuthStatus(Constants.AuditStatus.AUDITING_FAIL);
            driver.setBitState(BitStatesUtil.removeState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATING));
        }
        driverMapper.updateById(driver);
        //这里是抽离出来的多次用到的变量
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        String remark = auditAuthDto.getAuditRemark();
        //设置更新时间
        driverAuthMaterial.setUpdateTime(new Date());
        //设置
        driverAuthMaterial.setAuditUserId(loginIdAsLong);
        driverAuthMaterial.setAuditRemark(remark);
        //更新
        updateById(driverAuthMaterial);
        //保存司机材料认证日志
        DriverMaterialAuthLog driverMaterialAuthLog = new DriverMaterialAuthLog();
        //为什么这个id不用设置
//        driverMaterialAuthLog.setId(0L);
        driverMaterialAuthLog.setAuthMaterialId(auditAuthDto.getId());
        driverMaterialAuthLog.setRealAuthStatus(driverAuthMaterial.getRealAuthStatus());
        driverMaterialAuthLog.setAuditTime(new Date());
        //因为这里也用到了StpUtil.getLoginIdAsLong()  所以抽离出来一个变量
        driverMaterialAuthLog.setAuditUserId(loginIdAsLong);
        driverMaterialAuthLog.setAuditRemark(remark);
//        driverMaterialAuthLog.setCreateTime(new Date());
        driverMaterialAuthLog.setUpdateTime(new Date());
//        driverMaterialAuthLog.setDeleted(false);
//        driverMaterialAuthLog.setVersion(0);

        driverMaterialAuthLogService.save(driverMaterialAuthLog);
    }

    //
    @Override
    public void cancelAudit(CancelAuditDto cancelAuditDto) {
        //1.JSR303校验
        //2.业务检验
        //2.1 校验司机材料认证信息是否能查到
        DriverAuthMaterial driverAuthMaterial = getById(cancelAuditDto.getId());
        AssertUtil.isNotNull(driverAuthMaterial, GlobalExceptionCode.PARAM_ERROR);
        //2.2 校验司机材料认证信心的认证状态是否是1 是否是通过  只有通过才能被撤销
        AssertUtil.isTrue(Constants.AuditStatus.AUDITED_SUCCESS.equals(driverAuthMaterial.getRealAuthStatus()), GlobalExceptionCode.SERVICE_ERROR);
        //3.业务实现
        //将
        Driver driver = driverMapper.selectById(driverAuthMaterial.getDriverId());

        driver.setBitState(BitStatesUtil.removeState(driver.getBitState(), BitStatesUtil.addState(BitStatesUtil.OP_REAL_AUTHENTICATIONED, BitStatesUtil.OP_REAL_AUTHENTICATING)));
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        //3.1 修改driverAuthMaterial里面的认证状态
        driverAuthMaterial.setRealAuthStatus(Constants.AuditStatus.AUDITING_RECALL);
        //修改撤销认证的备注
        driverAuthMaterial.setAuditRemark(cancelAuditDto.getCancelAuditRemark());
        //修改
        driverAuthMaterial.setUpdateTime(new Date());
        //
        driverAuthMaterial.setAuditUserId(loginIdAsLong);
        //修改
        updateById(driverAuthMaterial);
        driverMapper.updateById(driver);
        //将撤销认证的东西放到司机材料认证的日志中
        DriverMaterialAuthLog driverMaterialAuthLog = new DriverMaterialAuthLog();
//        为什么不需要设置Id
//        driverMaterialAuthLog.setId(0L);
        driverMaterialAuthLog.setAuthMaterialId(cancelAuditDto.getId());
        driverMaterialAuthLog.setRealAuthStatus(driverAuthMaterial.getRealAuthStatus());
        driverMaterialAuthLog.setAuditTime(new Date());
        driverMaterialAuthLog.setAuditUserId(loginIdAsLong);
        driverMaterialAuthLog.setAuditRemark(cancelAuditDto.getCancelAuditRemark());
//        driverMaterialAuthLog.setCreateTime(new Date());
        driverMaterialAuthLog.setUpdateTime(new Date());
//        driverMaterialAuthLog.setDeleted(false);
//        driverMaterialAuthLog.setVersion(0);
        driverMaterialAuthLogService.save(driverMaterialAuthLog);

    }

    @Override
    public DriverAuthMaterial getYourselfAuthMaterial() {
        //1.参数校验 JSR303
        //1.前端没有传输参数 给后端 不需要参数校验
        //2.业务校验
        //2.1

        //3.业务实现
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        //3.1 查询最近时间的 自己的司机材料认证信息
        DriverAuthMaterial driverAuthMaterial = getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                        .eq(DriverAuthMaterial::getDriverId, loginIdAsLong)
                        .orderByDesc(DriverAuthMaterial::getCreateTime)
                , false);

        return driverAuthMaterial;
    }

    @Override
    public DriverWorkbenchVo getWorkbenchInfo() {
        //获取driverid
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        //通过DriverId查询，并且按照创建时间降序排序，取第一个
        DriverAuthMaterial driverAuthMaterial = getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, loginIdAsLong)
                .orderByDesc(DriverAuthMaterial::getCreateTime)
                , false);
        DriverWorkbenchVo driverWorkbenchVo = new DriverWorkbenchVo();
        //如果司机实名认证为空 司机实名状态不为认证成功，那么都会响应未认证
        if(ObjectUtils.isEmpty(driverAuthMaterial)||!Constants.AuditStatus.AUDITED_SUCCESS.equals(driverAuthMaterial.getRealAuthStatus())){
            driverWorkbenchVo.setRealAuthSuccess(false);
        }
        DriverSummary driverSummary = driverSummaryMapper.selectById(loginIdAsLong);
        BeanUtil.copyProperties(driverSummary,driverWorkbenchVo,false);

//        driverWorkbenchVo.setTodayIncome(driverSummary.getTodayIncome());
        return driverWorkbenchVo;
    }
}
